package com.study.hadoop.mapreduce.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @Author huang
 * @Date 2022/1/2 23:57
 * @Version 1.0
 */
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

    private int sum = 0;
    private IntWritable count = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        //将sum初始化为0
        sum = 0;
        //对迭代器进行遍历
        for (IntWritable value : values) {
            //将值进行相加
            sum += value.get();
        }
        //提交
        count.set(sum);
        context.write(key, count);
    }
}
